class Solution
{
public:
    vector<long long> minOperations(vector<int> &nums, vector<int> &queries)
    {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<long long> prefixSum(n + 1);
        prefixSum[0] = 0;
        for (int i = 1; i <= n; ++i)
        {
            prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
        }

        vector<long long> answer;
        for (int query : queries)
        {
            long long pos = lower_bound(nums.begin(), nums.end(), query) - nums.begin();
            long long frontSum = prefixSum[pos];
            long long backSum = prefixSum[n] - prefixSum[pos];
            answer.push_back(pos * query - frontSum + backSum - (n - pos) * query);
        }
        return answer;
    }
};